package com.feidee.data.email.util.mail;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.feidee.data.report.util.ToolUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feidee.data.email.dao.ConfDao;
import com.feidee.data.email.dao.impl.ConfDaoImpl;
import com.feidee.data.email.model.EConstant;
import com.feidee.data.email.model.EmailAddress;
import com.feidee.data.email.model.EmailLogModel;
import com.feidee.data.email.model.EmailResult;
import com.feidee.data.email.util.ApplicationContextUtil;
import com.feidee.data.email.util.EmailUtil;
import com.feidee.data.report.exception.DataDaoException;
import com.feidee.data.report.util.JsonUtil;

public class EmailDeal {

	private Logger LOG = LoggerFactory.getLogger(EmailDeal.class);
	private ConfDao confDao;
	private String model;
	private String email_key;

	public EmailDeal(String email_key, String model) {
		this.model = model;
		this.email_key = email_key;
		this.confDao = ApplicationContextUtil.getBean(ConfDaoImpl.class);
	}

	public EmailResult getEmailAndSend() {
		String time = EmailUtil.getNowTime();
		String str = "";
		long start = System.currentTimeMillis();
		EmailLogModel logs = new EmailLogModel();
		logs.setEmail_key(email_key);
		logs.setProcess("send");
		logs.setIndex(0);
		try {
			@SuppressWarnings({"deprecation", "resource"})
			HttpClient client = new DefaultHttpClient();
			String url = null;
			if (email_key.equals(EConstant.EMAIL_DAILY_KEY)) {
				url = EConstant.EMAIL_GET_DAILY_URL;
			} else if (email_key.equals(EConstant.EMAIL_WEEK_KEY)) {
				url = EConstant.EMAIL_GET_WEEK_URL;
			} else {
				LOG.error("没有匹配的邮件名称");
				logs.setMessage("Failed!There are no email key such as " + email_key);
				long end = System.currentTimeMillis();
				logs.setCost((int) (end - start));
				logs.setStarttime(time);
				EmailUtil.sendLog(logs, model);
				sendErrorMail("邮件名称异常,找不到匹配的邮件名称 " + email_key);
				return new EmailResult("-001", "邮件名称异常");
			}
			HttpGet get = new HttpGet(url);
			HttpResponse response = null;
			int attemptTimes = 3;
			for (int i = 1; i <= attemptTimes; i++) {
				try {
					if (i == attemptTimes) {
						Thread.sleep(1000 * 60L);// 第三次重试时，休眠1分钟
					}
					response = client.execute(get);
					LOG.info("成功连接到前端服务器");
					logs.setStatus("Succeed");
					logs.setMessage("Link to front-end succeed!");
					long end = System.currentTimeMillis();
					logs.setCost((int) (end - start));
					logs.setStarttime(time);
					EmailUtil.sendLog(logs, model);
					break;
				} catch (Exception e) {
					LOG.error("第" + i + "次前端服务连接失败：", e);
					if (i == attemptTimes) {
						logs.setStatus("Failed");
						logs.setMessage("Link to front-end failed " + i + " times!");
						long end = System.currentTimeMillis();
						logs.setCost((int) (end - start));
						logs.setStarttime(time);
						EmailUtil.sendLog(logs, model);
						sendErrorMail("前端服务连接失败!");
						return new EmailResult("-001", "前端服务连接失败");
					}
				}
			}
			HttpEntity entity = null;
			if (response != null) {
				entity = response.getEntity();
			}
			if (entity == null) {
				sendErrorMail("从前端获取到的内容为空！");
				return new EmailResult("-001", "http获取到内容为空");
			}
			InputStream in = entity.getContent();
			str = ToolUtils.convertStreamToString(in);
			logs.setStatus("Check");
			logs.setMessage("HTML Content: " + str);
			long end = System.currentTimeMillis();
			logs.setCost((int) (end - start));
			logs.setStarttime(time);
			EmailUtil.sendLog(logs, model);
			if ("failed".equals(str)) {
				sendErrorMail("前端返回邮件内容为failed!");
				return new EmailResult("-001", "http获取到的内容围绕false");
			}
			// LOG.info("成功获取"+email_key+"邮件信息,url:" + url);
			Map<String, Object> map = JsonUtil.jsonToMap(str);
			String topic = map.get(EConstant.EMAIL_TOPIC).toString();
			String content = map.get(EConstant.EMAIL_CONTENT).toString();
			@SuppressWarnings("unchecked")
			List<Map<String, String>> images = (List<Map<String, String>>) map.get(EConstant.EMAIL_IMAGES);
			@SuppressWarnings("unchecked")
			List<Map<String, String>> files = (List<Map<String, String>>) map.get(EConstant.EMAIL_FILES);

			if (topic == null || "".equals(topic.trim()) || content == null || "".equals(content.trim())) {
				sendErrorMail("前端返回邮件内容异常。【 " + str + " 】");
				return new EmailResult("-001", "http获取到的邮件内容异常");
			}
			if (files == null || files.size() < 1) {
				RMailSender.sendHtmlMail(buildNorMailInfo(email_key, topic, content, images));
			} else {
				RMailSender.sendHtmlMail(buildNorMailInfo(email_key, topic, content, images, files));
			}
			LOG.info("成功从" + url + "获取到邮件,并发送");
			logs.setStatus("SuccessFully");
			logs.setMessage("None");
			end = System.currentTimeMillis();
			logs.setCost((int) (end - start));
			logs.setStarttime(time);
			EmailUtil.sendLog(logs, model);
			return new EmailResult("000", "Successfully");
		} catch (Exception e) {
			LOG.error("获取邮件发送失败：", e);
			logs.setStatus("Failed");
			logs.setMessage(e.getMessage());
			long end = System.currentTimeMillis();
			logs.setCost((int) (end - start));
			logs.setStarttime(time);
			EmailUtil.sendLog(logs, model);
			try {
				sendErrorMail("前端返回邮件内容异常。返回邮件内容为：【 " + str + " 】");
			} catch (Exception e1) {
				LOG.info("日报/周报发送错误邮件失败！");
			}
			return new EmailResult("-001", e.getMessage());
		}

	}

	private RMailInfo buildNorMailInfo(String email_key, String topic, String content, List<Map<String, String>> list)
			throws DataDaoException {
		RMailInfo mailInfo = new RMailInfo();
		mailInfo.setMailServerHost(EConstant.EMAIL_SEND_HOST);
		mailInfo.setMailServerPort(EConstant.EMAIL_SEND_PORT);
		List<Image> images = new ArrayList<Image>();
		for (Map<String, String> map : list) {
			Image image = new Image(map.get("id").toString(), map.get("url").toString());
			images.add(image);
		}
		mailInfo.setImages(images);

		mailInfo.setValidate(true);
		mailInfo.setUserName(EConstant.EMAIL_SEND_USER);
		mailInfo.setPassword(EConstant.EMAIL_SEND_PASSWORD);
		mailInfo.setFromAddress(EConstant.EMAIL_SEND_FROM);
		EmailAddress adds = confDao.getEmailAddress(email_key, "normal");
		mailInfo.setToAddress(adds.getToAddresses());
		mailInfo.setCcAddress(adds.getCcAddresses());
		mailInfo.setBccAddress(adds.getBccAddresses());
		mailInfo.setSubject(topic);
		mailInfo.setContent(content);
		return mailInfo;
	}

	private RMailInfo buildNorMailInfo(String email_key, String topic, String content, List<Map<String, String>> list,
			List<Map<String, String>> file) throws DataDaoException {
		RMailInfo mailInfo = new RMailInfo();
		mailInfo.setMailServerHost(EConstant.EMAIL_SEND_HOST);
		mailInfo.setMailServerPort(EConstant.EMAIL_SEND_PORT);
		List<Image> images = new ArrayList<Image>();
		for (Map<String, String> map : list) {
			Image image = new Image(map.get("id").toString(), map.get("url").toString());
			images.add(image);
		}
		mailInfo.setImages(images);
		List<File> attachFiles = new ArrayList<File>();
		for (Map<String, String> map : file) {
			File fileTmp = new File(map.get("url").toString());
			attachFiles.add(fileTmp);
		}
		mailInfo.setAttachFileNames(attachFiles);

		mailInfo.setValidate(true);
		mailInfo.setUserName(EConstant.EMAIL_SEND_USER);
		mailInfo.setPassword(EConstant.EMAIL_SEND_PASSWORD);
		mailInfo.setFromAddress(EConstant.EMAIL_SEND_FROM);
		EmailAddress adds = confDao.getEmailAddress(email_key, "normal");
		mailInfo.setToAddress(adds.getToAddresses());
		mailInfo.setCcAddress(adds.getCcAddresses());
		mailInfo.setBccAddress(adds.getBccAddresses());
		mailInfo.setSubject(topic);
		mailInfo.setContent(content);
		return mailInfo;
	}

	private RMailInfo buildErrorMailInfo(String email_key, String topic, String content) throws DataDaoException {
		RMailInfo mailInfo = new RMailInfo();
		mailInfo.setMailServerHost(EConstant.EMAIL_SEND_HOST);
		mailInfo.setMailServerPort(EConstant.EMAIL_SEND_PORT);
		mailInfo.setValidate(true);
		mailInfo.setUserName(EConstant.EMAIL_SEND_USER);
		mailInfo.setPassword(EConstant.EMAIL_SEND_PASSWORD);
		mailInfo.setFromAddress(EConstant.EMAIL_SEND_FROM);
		EmailAddress adds = confDao.getEmailAddress(email_key, "error");
		mailInfo.setToAddress(adds.getToAddresses());
		mailInfo.setCcAddress(adds.getCcAddresses());
		mailInfo.setBccAddress(adds.getBccAddresses());
		mailInfo.setSubject(topic);
		mailInfo.setContent(content);
		return mailInfo;
	}

	public void sendErrorMail(String errorMess) throws Exception {
		String time = EmailUtil.getNowTime();
		long start = System.currentTimeMillis();
		EmailLogModel logs = new EmailLogModel();
		logs.setEmail_key(email_key);
		logs.setProcess("send");
		logs.setIndex(1);

		try {
			String topic = "";
			String content = "";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
			String dateStr = sdf.format(new Date());
			if ("business_analysis".equals(email_key)) {
				topic = "【《" + dateStr + "》业务日报未能准时发送】【需处理】";
				content = "<p class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">各位同事好，<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n"
						+ "<o:p></o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">截止北京时间<span lang=\"EN-US\">14</span>：<span lang=\"EN-US\">00</span><span style=\"color:black\">，</span><span style=\"color:red\">业务日报</span><span style=\"color:black\">数据同步仍未完成，邮件未能准时发送</span><span lang=\"EN-US\"  style=\"color:red\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ;;color:black \">请开发尽快处理，</span><span style=\"  ; ; ;;color:red \">若评估处理时效较长，请及时手工准备数据发送</span><span style=\"  ; ; ;;color:black \">，谢谢<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\">"
						+ "<span style=\"  ; ; ;;color:black \">详细错误信息： </span>"
						+ errorMess
						+ "<span></span></p>"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">附：邮件收发人<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<table class=\"MsoNormalTable\"  border=\"0\"  cellspacing=\"0\"  cellpadding=\"0\"  width=\"531\"  style=\"width:398.5pt;margin-left:18.0pt;border-collapse:collapse\">\n"
						+ "<tbody>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">发送邮箱<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">随手科技数据门户（<span lang=\"EN-US\"><a href=\"mailto:sskj_data@suishouji.com\"  style=\"color: blue; text-decoration: underline;\">sskj_data@suishouji.com</a></span>）<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;border-top:none;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">收件人<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">谷风<span lang=\"EN-US\"> (<a href=\"mailto:gufeng@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">gufeng@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\" font-size:8.0pt ; ; ; ; \">HSIN-SHU-BRANDEN CHEN (<a href=\"mailto:branden_chen@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">branden_chen@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">吴晓慧<span lang=\"EN-US\"> (<a href=\"mailto:xiaohui_wu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">xiaohui_wu@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">李勇<span lang=\"EN-US\"> (<a href=\"mailto:liyong@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">liyong@kingdee.com</a>)<o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;border-top:none;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">抄送栏<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">刘健<span lang=\"EN-US\"> (<a href=\"mailto:emil@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">emil@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">孙浩<span lang=\"EN-US\"> (<a href=\"mailto:hao_sun@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">hao_sun@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">刘红涛<span lang=\"EN-US\"> (<a href=\"mailto:hongtao_liu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">hongtao_liu@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">马蔷<span lang=\"EN-US\">(<a href=\"mailto:hingis_ma@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">hingis_ma@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">张志华<span lang=\"EN-US\"> (<a href=\"mailto:zhihua_zhang@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">zhihua_zhang@kingdee.com</a>)<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">梁莉萍（<span lang=\"EN-US\"><a href=\"mailto:liping_liang@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">liping_liang@kingdee.com</a></span>）<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">吴睿（<span lang=\"EN-US\"><a href=\"mailto:rui_wu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">rui_wu@kingdee.com</a></span>）<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  valign=\"top\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;border-top:none;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">密送栏<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">侯卫民<span lang=\"EN-US\">(<a href=\"mailto:weimin_hou@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">weimin_hou@kingdee.com</a>);<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">徐定坚<span lang=\"EN-US\">(<a href=\"mailto:dingjian_xu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">dingjian_xu@kingdee.com</a>);<o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "</tbody>\n" + "</table>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
						+ "<div class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\">\n"
						+ "<hr size=\"1\"  width=\"210\"  style=\"width:157.5pt\"  noshade=\"\"  align=\"left\" />\n"
						+ "</span></div>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"color:#7F7F7F\"><a href=\"http://data.feidee.com/bigdata/own/index/index.jsp\"  style=\"color: blue; text-decoration: underline;\"><span lang=\"EN-US\"  style=\"font-family:宋体;color:#7F7FFF\"><span lang=\"EN-US\">随手科技数据门户</span></span></a><o:p></o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"font-family:宋体;color:#7F7F7F\">大数据云事业部</span><span style=\"color:#7F7F7F\">\n"
						+ "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
						+ "</div>\n" + "</div></div>";
			} else if ("business_analysis_week_report".equals(email_key)) {
				topic = "【《" + dateStr + "》业务周报未能准时发送】【需处理】";
				content = "<p class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">各位同事好，<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n"
						+ "<o:p></o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">截止北京时间<span lang=\"EN-US\">14</span>：<span lang=\"EN-US\">00</span><span style=\"color:black\">，</span><span style=\"color:red\">业务周报</span><span style=\"color:black\">数据同步仍未完成，邮件未能准时发送</span><span lang=\"EN-US\"  style=\"color:red\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ;;color:black \">请开发尽快处理，</span><span style=\"  ; ; ;;color:red \">若评估处理时效较长，请及时手工准备数据发送</span><span style=\"  ; ; ;;color:black \">，谢谢<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\">"
						+ "<span style=\"  ; ; ;;color:black \">详细错误信息： </span>"
						+ errorMess
						+ "<span></span></p>"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"  ; ; ; \">附：邮件收发人<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<table class=\"MsoNormalTable\"  border=\"0\"  cellspacing=\"0\"  cellpadding=\"0\"  width=\"531\"  style=\"width:398.5pt;margin-left:18.0pt;border-collapse:collapse\">\n"
						+ "<tbody>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">发送邮箱<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">随手科技数据门户（<span lang=\"EN-US\"><a href=\"mailto:sskj_data@suishouji.com\"  style=\"color: blue; text-decoration: underline;\">sskj_data@suishouji.com</a></span>）<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;border-top:none;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">收件人<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">谷风<span lang=\"EN-US\"> (<a href=\"mailto:gufeng@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">gufeng@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\" font-size:8.0pt ; ; ; ; \">HSIN-SHU-BRANDEN CHEN (<a href=\"mailto:branden_chen@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">branden_chen@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">吴晓慧<span lang=\"EN-US\"> (<a href=\"mailto:xiaohui_wu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">xiaohui_wu@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">李勇<span lang=\"EN-US\"> (<a href=\"mailto:liyong@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">liyong@kingdee.com</a>)<o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;border-top:none;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">抄送栏<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">刘健<span lang=\"EN-US\"> (<a href=\"mailto:emil@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">emil@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">孙浩<span lang=\"EN-US\"> (<a href=\"mailto:hao_sun@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">hao_sun@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">刘红涛<span lang=\"EN-US\"> (<a href=\"mailto:hongtao_liu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">hongtao_liu@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">马蔷<span lang=\"EN-US\">(<a href=\"mailto:hingis_ma@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">hingis_ma@kingdee.com</a>);\n"
						+ "<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">张志华<span lang=\"EN-US\"> (<a href=\"mailto:zhihua_zhang@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">zhihua_zhang@kingdee.com</a>)<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">梁莉萍（<span lang=\"EN-US\"><a href=\"mailto:liping_liang@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">liping_liang@kingdee.com</a></span>）<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">吴睿（<span lang=\"EN-US\"><a href=\"mailto:rui_wu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">rui_wu@kingdee.com</a></span>）<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "<tr style=\"height:2.85pt\">\n"
						+ "<td width=\"116\"  valign=\"top\"  style=\"width:86.65pt;border:solid windowtext 1.0pt;border-top:none;background:#95B3D7;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">密送栏<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "</td>\n"
						+ "<td width=\"416\"  valign=\"top\"  style=\"width:11.0cm;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:2.85pt\">\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">侯卫民<span lang=\"EN-US\">(<a href=\"mailto:weimin_hou@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">weimin_hou@kingdee.com</a>);<o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoListParagraph\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\" font-size:8.0pt ; ; ; ; \">徐定坚<span lang=\"EN-US\">(<a href=\"mailto:dingjian_xu@kingdee.com\"  style=\"color: blue; text-decoration: underline;\">dingjian_xu@kingdee.com</a>);<o:p></o:p></span></span></p>\n"
						+ "</td>\n" + "</tr>\n" + "</tbody>\n" + "</table>\n"
						+ "<p class=\"MsoNormal\"  style=\"text-indent: 21pt; margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"  ; ; ; \"><o:p>&nbsp;</o:p></span></p>\n"
						+ "<div class=\"MsoNormal\"  align=\"left\"  style=\"text-align: justify; margin: 0cm 0cm 0.0001pt; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\">\n"
						+ "<hr size=\"1\"  width=\"210\"  style=\"width:157.5pt\"  noshade=\"\"  align=\"left\" />\n"
						+ "</span></div>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"  style=\"color:#7F7F7F\"><a href=\"http://data.feidee.com/bigdata/own/index/index.jsp\"  style=\"color: blue; text-decoration: underline;\"><span lang=\"EN-US\"  style=\"font-family:宋体;color:#7F7FFF\"><span lang=\"EN-US\">随手科技数据门户</span></span></a><o:p></o:p></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span style=\"font-family:宋体;color:#7F7F7F\">大数据云事业部</span><span style=\"color:#7F7F7F\">\n"
						+ "<span lang=\"EN-US\"><o:p></o:p></span></span></p>\n"
						+ "<p class=\"MsoNormal\"  style=\"margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 10.5pt; font-family: Calibri, sans-serif;\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n"
						+ "</div>\n" + "</div></div>";
			}
			RMailSender.sendHtmlMail(buildErrorMailInfo(email_key, topic, content));
			logs.setStatus("SuccessFully");
			logs.setMessage("None");
			long end = System.currentTimeMillis();
			logs.setCost((int) (end - start));
			logs.setStarttime(time);
			EmailUtil.sendLog(logs, model);
		} catch (Exception e) {
			e.printStackTrace();
			logs.setStatus("Failed");
			logs.setMessage(e.getMessage());
			long end = System.currentTimeMillis();
			logs.setCost((int) (end - start));
			logs.setStarttime(time);
			EmailUtil.sendLog(logs, model);
			throw new Exception(e);

		}

	}
}